//
//  TTHttpRequest.m
//  ExhibitionHall
//
//  Created by 王洋 on 2019/6/3.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "AFNetworking.h"
#import "TTHttpRequest.h"
#import "TTUDIDManager.h"
@implementation TTHttpRequest

- (void)getStart {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];

    manager.requestSerializer.timeoutInterval = 10.0;
    [manager.requestSerializer setValue:[TTUserModel sharedObject].accessToken forHTTPHeaderField:@"Authorization"];
    [manager.requestSerializer setValue:@"application/text;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"x-platform"];
    [manager.requestSerializer setValue:APP_VERSION forHTTPHeaderField:@"x-version"];
    [manager.requestSerializer setValue:@"store" forHTTPHeaderField:@"x-client"];
    [manager.requestSerializer setValue:[TTUDIDManager getUniqueDeviceID] forHTTPHeaderField:@"x-equCode"];
    
    WEAKSELF
    [manager GET:self.url parameters:self.getData progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *jsonDict = [Utilities dictionaryWithJsonString:responseObject];
        self.finishBlock(jsonDict);
        
        BOOL isLogin = [[jsonDict valueForKey:@"login"] boolValue];
        if (!isLogin && ![weakSelf.url containsString:@"/ipad/login"]) {
            [weakSelf goLoginWithMessage:[jsonDict valueForKey:@"message"]];
        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        self.failedBlock(error);

    }];
    
}

- (void)postStart {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];

    manager.requestSerializer.timeoutInterval = 10.0;

    [manager.requestSerializer setValue:[TTUserModel sharedObject].accessToken forHTTPHeaderField:@"Authorization"];
    [manager.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"x-platform"];
    [manager.requestSerializer setValue:APP_VERSION forHTTPHeaderField:@"x-version"];
    [manager.requestSerializer setValue:@"exhibittionHall" forHTTPHeaderField:@"x-client"];
    [manager.requestSerializer setValue:[TTUDIDManager getUniqueDeviceID] forHTTPHeaderField:@"x-equCode"];
    
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    WEAKSELF
    [manager POST:self.url parameters:self.postData progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                NSDictionary *jsonDict = [Utilities dictionaryWithJsonString:responseObject];
                self.finishBlock(jsonDict);
        
        BOOL isLogin = [[jsonDict valueForKey:@"login"] boolValue];
        if (!isLogin && ![weakSelf.url containsString:@"/ipad/login"]) {
            [weakSelf goLoginWithMessage:[jsonDict valueForKey:@"message"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
            self.failedBlock(error);

    }];
    
}

- (void)goLoginWithMessage:(NSString *)msg {
    
    [WHToast showErrorWithMessage:msg duration:TT_GLOBAL_DURATION_TIME finishHandler:^{
        
    }];
    [[TTUserModel sharedObject] initialization]; //userModel清空当前用户的所有信息
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[NSClassFromString(@"LoginViewController") new]];
    [UIApplication sharedApplication].keyWindow.rootViewController = navController;
    navController.navigationBar.hidden = YES;
    
}
















@end
